
var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['', '', ''],
    token:'',
    selectAddress : '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let address = '';
    var region = '';
    console.log(options);
    if (options.selectAddress){
      address = JSON.parse(options.selectAddress);
      region = address.city.split('-');
    }
    var token = wx.getStorageSync('token');       //用户的token信息
    that.setData({
      selectAddress : address,
      token:token,
      region:region,
    })
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 删除地址
   */
  deletAddressClick:function(){

    var that = this;

    wx.showModal({
      title: '提示',
      content: '确认删除该地址吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          //调用删除地址接口

          wx.request({
            url: apiHost + '/mobile/users/address/del',
            data: {
              addrId: that.data.selectAddress.id,
            },
            success: function (res) {
              if (res.data.code == '10000') {
                wx.showToast({
                  title: '删除成功',
                  duration: 1000,
                })
                wx.navigateBack({
                  url: "../receivingAddressList / receivingAddressList",
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                })
              }
            }
          })


        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },

  /**
   * 添加新地址或编辑保存点击
   */
  saveAddressClick:function(e){

    console.log('123',e);
    var that = this;

    var name = e.detail.value.userName;
    var phone = e.detail.value.userPhone;
    var city = e.detail.value.userCity;
    var detail = e.detail.value.userCityDetail;

    if(name.length < 1 ||phone.length < 1||city.length < 3||detail.length < 1){
        wx.showToast({
          icon:'none',
          title: '请填写完整信息',
        })
    }

    console.log(name,phone,city,detail);
    if(that.data.selectAddress != ''){
      that.editSave(name, phone, city, detail);
    }else{
      that.addNewAddress(name, phone, city, detail);
    }
  },
  /**
   * 添加新地址接口
   */
  addNewAddress: function (name, phone, city, detail){
    var that = this;
    wx.request({
      url: apiHost + '/mobile/users/address/add',
      data:{
        token: wx.getStorageSync('token'),
        appId: 'xcx',
        receiverName: name,
        city: city,
        detail: detail,
        receiverPhone: phone,
      },
      success:function(res){
        if (res.data.code == '10000') {
          wx.showToast({
            title: '保存成功',
          })
          wx.navigateBack({
            url: "../receivingAddressList / receivingAddressList",
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none',
          })
        }
      }
    })
  },
  /**
   * 编辑保存
   */
  editSave: function (name, phone, city, detail){

    var that = this;
    wx.request({
      url: apiHost + '/mobile/users/address/edit',
      data:{
        token : that.data.token,
        appId : 'xcx',
        addrId : that.data.selectAddress.id,
        receiverName :name,
        city:city,
        detail:detail,
        receiverPhone:phone,
      },
      success:function(res){
        if(res.data.code == '10000'){
          wx.showToast({
            title: '保存成功',
          })
          wx.navigateBack({
            url: "../receivingAddressList / receivingAddressList",
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})